package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniuxy.controller.form.PriorityForm;
import com.woniuxy.controller.info.PriorityInfo;
import com.woniuxy.controller.info.UserInfo;
import com.woniuxy.dao.UserDao;
import com.woniuxy.model.Defectpriority;
import com.woniuxy.dao.DefectpriorityDao;
import com.woniuxy.model.User;
import com.woniuxy.service.DefectpriorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.ChangeDefectpriority;
import com.woniuxy.utils.CopyUtil;
import com.woniuxy.utils.TimeMethodsutil;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马世亮
 * @since 2022-02-15
 */
@Service
public class DefectpriorityServiceImpl extends ServiceImpl<DefectpriorityDao, Defectpriority> implements DefectpriorityService {

    @Resource
    private DefectpriorityDao defectpriorityDao;
    @Resource
    private UserDao userDao;

    @Override
    public List<PriorityInfo> findAll() {

        List<Defectpriority> list = defectpriorityDao.selectAll();
        List<PriorityInfo> priorityInfo = ChangeDefectpriority.ChangeToInfo(list);
        return priorityInfo;
    }

    @Override
    public Integer add(PriorityForm priorityForm) {
        priorityForm.setDefectpriorityState(1);
        UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account_id", userInfo.getId()));
        Defectpriority defectpriority = CopyUtil.copyOne(priorityForm, Defectpriority.class);
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();
        defectpriority.setDefectpriorityCreatetime(nowTimeToString);
        defectpriority.setDefectpriorityCreatenameId(user.getUId());
        defectpriority.setDefectpriorityCreatename(user.getUName());
        int insert = defectpriorityDao.insert(defectpriority);
        return insert;
    }

    @Override
    public Integer delectByIds(List<Integer> ids) {

        int i = this.baseMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public Integer changeState(PriorityForm priorityFor) {
        String state = priorityFor.getDefectpriorityNewState();

        if(state.equals("有效")){
            priorityFor.setDefectpriorityState(0);

        }else if(state.equals("禁用")){
            priorityFor.setDefectpriorityState(1);

        }
        Defectpriority defectpriority = CopyUtil.copyOne(priorityFor, Defectpriority.class);
        UpdateWrapper<Defectpriority> wrapper = new UpdateWrapper<>();
        wrapper.eq("defectPriority_name",defectpriority.getDefectpriorityName()).set("defectPriority_state",defectpriority.getDefectpriorityState());
        int update = defectpriorityDao.update(null, wrapper);
        return update;
    }

    @Override
    public boolean change(PriorityForm priorityForm) {
        UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account_id", userInfo.getId()));
        String nowTimeToString = TimeMethodsutil.getNowTimeToString();

        Defectpriority defectpriority = new Defectpriority();
        defectpriority.setDefectpriorityId(priorityForm.getDefectpriorityId());
        defectpriority.setDefectpriorityNum(priorityForm.getDefectpriorityNum());
        defectpriority.setDefectpriorityName(priorityForm.getDefectpriorityName());
        defectpriority.setDefectpriorityChangenameId(user.getUId());
        defectpriority.setDefectpriorityChangename(user.getUName());
        defectpriority.setDefectpriorityChangetime(nowTimeToString);

        int i = defectpriorityDao.updateById(defectpriority);

        if (i>0){
            return true;
        }else {
            return false;
        }

    }


}
